	$('#Commonheader').load('/pc/Commonheader.html');
	$('#Commonfooter').load('/pc/CommonFooter.html');
	
$(function(){

	getVcode();
	$("#yzm").on("click",function(){
		getVcode();
		
	})
	
	function getVcode(){
		$.ajax({
			type:"post",
			url:"/adminLogin/vcode",
			success:function(data){
				if(data.code!=0){
					$("#yzm").attr('src',data.path);
					
				}
			}
		})
	}

    $('#banner_section>div').on('click','a',function(e){
        e.preventDefault();
        var showId = $(this).attr('href');
        $(showId).addClass('active').siblings('.active').removeClass('active');
        if( showId == '#appraisement' ){
            $('#banner_section').removeClass('banner_section_carInquire').addClass('banner_section_appraisement');
        }
        if( showId == '#carInquire' ){
            $('#banner_section').removeClass('banner_section_appraisement').addClass('banner_section_carInquire');
        }
    })
    
    $('#chooseCar_pp_page>li').on('click','a',function(e){
        e.preventDefault();
        var id = $(this).attr('href');
        var toTop = $(`#chooseCar_pp_list>li[id=${id}]`).position().top;
        $('#chooseCar_pp_list_parent').scrollTop(toTop);
    })
    
    /**
     * 车架号格式验证
     * */
    $('#vin').blur(function(){
    	var vin = $('#vin').val();
    	var vinReg = /^[A-Za-z0-9]{17}$/;
    	if( vinReg.test(vin) == false ){
			$('#vin').addClass('err');
			$('#vin').val('');
			$('#vin').attr('placeholder','您输入的号码有误');
    	}else{
    		$('#vin').removeClass('err');
    	}
    })
    $('#vin').focus(function(){
    	$('#vin').removeClass('err');
		$('#vin').attr('placeholder','请输入VIN号');
		getVcode();
    })
    
    
    
    
    $('#carInquire_btn').click(function(){
    	var vin = $('#vin').val();
    	var vcode = $('#vcode').val();
        
        if( vin != '' && vin != undefined && vin != null && vcode != '' && vcode != undefined && vcode != null){
        	//window.sessionStorage['vin'] = vin;
        	//window.sessionStorage['vcode'] = vcode;
        	$.ajax({
                url:'/che300/identifyModelByVIN',
                type:'get',
                data:{
                	vin:vin,
                	vcode:vcode
                	},
                success:function(data){
                    console.log(data);
                   if(data.ret_code == 1){
                	   
                	   window.sessionStorage['pp'] = data.series_name;
                	   window.sessionStorage['cxmz'] = data.series_name;
                	   window.sessionStorage['cx'] = data.model_name;
                	   window.sessionStorage['pfbz'] = data.model_emission_standard;
                	   window.sessionStorage['ccrq'] = data.model_year;
                	   window.sessionStorage['cjmc'] = data.brand_name;
                	   window.sessionStorage['xcj'] = data.model_price;
                	   window.location.href = '/home/cartypeQuery'
                   }else{
                	   
                	   if(data.ret_msg == '查询失败'){
                		    $('#vin').addClass('err');
	   	           			$('#vin').val('');
	   	           			$('#vin').attr('placeholder','您输入的号码有误');
	   	           			$('#vin').focus(function(){
	   	           		    	$('#vin').removeClass('err');
	   	           				$('#vin').attr('placeholder','请输入VIN号');
	   	           				getVcode();
	   	           		    })
                	   }else{
                		   
                		$('#vcode').addClass('err');
                		$('#vcode').parent().addClass('err');
   	           			$('#vcode').val('');
   	           			$('#vcode').attr('placeholder','验证码错误');
   	           			$('#vcode').focus(function(){
   	           		    	$('#vcode').removeClass('err');
   	           		    	$('#vcode').parent().removeClass('err');
   	           				$('#vcode').attr('placeholder','请输入验证码');
   	           				getVcode();
   	           		    })
                	   }
 	    
                   }
                }
            })
        	
        }else{
        	console.log(0);
        }
    	
    })
    

    
    $.ajax({
    	url:'/che300/getCarBrandList',
    	type:'get',
    	success:function(data){
    		var brandMap = {
        			A: [],
        			B: [],
        			C: [],
        			D: [],
        			E: [],
        			F: [],
        			G: [],
        			H: [],
        			I: [],
        			J: [],
        			K: [],
        			L: [],
        			M: [],
        			N: [],
        			O: [],
        			P: [],
        			Q: [],
        			R: [],
        			S: [],
        			T: [],
        			U: [],
        			V: [],
        			W: [],
        			X: [],
        			Y: [],
        			Z: []
        		}
    		
    		var chooseCar_pp_listHTML = '';
    		for (var i = 0; i < data.length; i++) {
				if (brandMap[data[i].initial]) {
					brandMap[data[i].initial].push(data[i])
				}
			}
    		
    		for (var k in brandMap) {
				if (brandMap[k].length > 0) {
					chooseCar_pp_listHTML+=`
					<li id='${k}'>
	                    <p class='chooseCar_tipStyle'>${k}</p>
	                    <ul id="chooseCar_pp_list_${k}">
					`
    				for (var i = 0; i < brandMap[k].length; i++) {
    					
    					chooseCar_pp_listHTML += `
	                            <li class='chooseCar_liStyle' data_id='${brandMap[k][i].brand_id}' data_name='${brandMap[k][i].brand_name}'>${brandMap[k][i].brand_name}</li>
    					`
    				}
					chooseCar_pp_listHTML+=`
						</ul>
                    </li>
					`
				}
			}
    		$('#chooseCar_pp_list').html(chooseCar_pp_listHTML);
    		
    	}
    })
    
    $('#chooseCar').click(function(e){
    	e.stopPropagation();
         $('#chooseCar_pp').addClass('active');
    })
    
    
    $('#chooseCar_pp_list').on('click','li',function(e){
    	e.stopPropagation();
    	var brand_id = $(this).attr('data_id');
    	var series_group_name = $(this).attr('data_name');
    	$.ajax({
    		url:'/che300/getCarSeriesList',
    		type:'get',
    		data:{brandId:brand_id},
    		success:function(data){
    			var chooseCar_cx_listHTML = '';
    			chooseCar_cx_listHTML+=`
    			<p class='chooseCar_tipStyle'>${series_group_name}</p>
    			`;
    			for(var i=0;i<data.length;i++){
    				chooseCar_cx_listHTML+=`
    					<li class='chooseCar_liStyle' data_id='${data[i].series_id}'>${data[i].series_name}</li>
    				`
    			}
    			$('#chooseCar_cx_list').html(chooseCar_cx_listHTML)
    		}
    	})
    	
    	$('#chooseCar_cx').addClass('active');
    	
    	if( $('#chooseCar_xh').hasClass('active') ){
    		$('#chooseCar_xh').removeClass('active')
    	}
    	
    })
    
    $('#chooseCar_cx_list').on('click','li',function(e){
    	e.stopPropagation();
    	var series_id = $(this).attr('data_id');
    	$.ajax({
    		url:'/che300/getCarModelList',
    		type:'get',
    		data:{seriesId:series_id},
    		success:function(data){
    			
    			var chooseCar_xh_listHTML = '';
    			for(var i=0;i<data.length;i++){
    				chooseCar_xh_listHTML+=`
    					<li class='chooseCar_liStyle' model_price='${data[i].model_price}' discharge_standard='${data[i].discharge_standard}' liter='${data[i].liter}' gear_type='${data[i].gear_type}' model_id='${data[i].model_id}' data_name='${data[i].model_name}' minYear='${data[i].min_reg_year}' maxYear='${data[i].max_reg_year}'>${data[i].model_name}</li>
    				`
    			}
    			$('#chooseCar_xh_list').html(chooseCar_xh_listHTML)
    		}
    	})
    	$('#chooseCar_xh').addClass('active');
    })
    
    //当车型号被选择时，将年份和名字代入页面中，并影藏弹框
    $('#chooseCar_xh').on('click','li',function(e){
    	e.stopPropagation();
    	var model_name = $(this).attr('data_name');
    	var model_id = $(this).attr('model_id');
    	var minYear = $(this).attr('minYear');
    	var maxYear = $(this).attr('maxYear');
    	
    	var gear_type = $(this).attr('gear_type');//自动还是手动
    	var liter = $(this).attr('liter');//排量
    	var discharge_standard = $(this).attr('discharge_standard');//排放标准
    	var model_price = $(this).attr('model_price');//新车售价
    	
    	$('#chooseCar>p').html(model_name);
    	$('#chooseCar>p').attr('minYear',minYear);
    	$('#chooseCar>p').attr('maxYear',maxYear);
    	$('#chooseCar>p').attr('model_id',model_id);
    	
    	$('#chooseCar>p').attr('gear_type',gear_type);
    	$('#chooseCar>p').attr('liter',liter);
    	$('#chooseCar>p').attr('discharge_standard',discharge_standard);
    	$('#chooseCar>p').attr('model_price',model_price);
    	
    	
    	$('#chooseCar_cx').removeClass('active');
		$('#chooseCar_xh').removeClass('active');
		$('#chooseCar_pp').removeClass('active');
    })
    
    //当点击区域范围不处于品牌选择车型选择，型号选择范围外，弹框将全部收起。
    $(document).click(function(e){  
    	var $this = $(e.target); 
    	if($this.attr('id') != "chooseCar_cx" || $this.attr('id') != "chooseCar_pp" || $this.attr('id') != "chooseCar"){
			$('#chooseCar_cx').removeClass('active');
			$('#chooseCar_xh').removeClass('active');
			$('#chooseCar_pp').removeClass('active');
    	}
    	if( $this.attr('id') == "chooseCar" ){
    		$('#chooseCar_pp').addClass('active');
    	}
    });
    
    //当请选择年份被点击时，判断是否选择车型，选择后才可以拿到车型年份，否则不可点击。
    $('#chooseCar_year').click(function(e){
    	e.stopPropagation();
    	
    	if( $('#chooseCar>p').html()!='请选择车型' ){
    		var minYear = $('#chooseCar>p').attr('minYear');
    		var maxYear = $('#chooseCar>p').attr('maxYear');
    		
    		var chooseCar_year_listHTML = '';
    		for(var i = minYear; i <= maxYear; i++){
    			chooseCar_year_listHTML += `
    				<li class='chooseCar_liStyle' year='${i}'>${i}年</li>
    			`
    		}
    		$('#chooseCar_year_list_list').html(chooseCar_year_listHTML);
    		
    		$('#chooseCar_year_list').addClass('active');
    		
    	}
    	
    })
    
    //年份被点击的时候，将内容显示到页面上
    $('#chooseCar_year_list_list').on('click','li',function(e){
    	e.stopPropagation();
    	var monHTML = '';
    	var year = $(this).attr('year');
    	for(var i=1;i<=12;i++){
    		monHTML += `
    			<li class='chooseCar_liStyle' year='${year}' mon='${i}'>${i}月</li>
    		`
    	}
    	$('#chooseCar_month_list_list').html(monHTML);
    	
    	$('#chooseCar_month_list').addClass('active');
    	
    })
    $('#chooseCar_month_list').on('click','li',function(e){
    	e.stopPropagation();
    	
    	$('#chooseCar_year_list').removeClass('active');
    	$('#chooseCar_month_list').removeClass('active');
    	var year = $(this).attr('year');
    	var mon = $(this).attr('mon');
    	$('#chooseCar_year>p').html( year+'年'+mon+'月' );
    	$('#chooseCar_year>p').attr('year',year);
    	$('#chooseCar_year>p').attr('mon',mon);
    })
    
    //查询所有城市
    $.ajax({
    	url:'/che300/getAllCity',
    	type:'get',
    	success:function(data){
    		var provIdList = repeat(data);
    		provIdList.sort(compare);
    		
    		//省
    		var prov = [];
    		for(var c=0;c<data.length;c++){
    			for(var i=0;i<provIdList.length;i++){
    				
    				if(data[c].prov_id == provIdList[i]){
    					prov[i] = {};
    					prov[i].prov_name = data[c].prov_name;
    					prov[i].prov_id = data[c].prov_id;
    					prov[i].city = [];
    				}
    				
        		}
    		}

    		var provHTML = '';
    		for(var i=0;i<prov.length;i++){
    			provHTML += `
    				<li class='chooseCar_liStyle' prov_id='${prov[i].prov_id}' prov_name='${prov[i].prov_name}'>${prov[i].prov_name}</li>
    			`
    		}
    		$('#chooseCar_prov_list').html(provHTML);
    		
    		$('#chooseCar_local').click(function(e){
    			e.stopPropagation();
    			$('#chooseCar_prov').addClass('active');
    		})
    		
    		$('#chooseCar_prov_list').on('click','li',function(e){
    			e.stopPropagation();
    			var pid = $(this).attr('prov_id');
    			var pname = $(this).attr('prov_name');
    			var cityHTML = '';
    			for(var i=0;i<data.length;i++){
    				if(data[i].prov_id == pid){
    					cityHTML += `
    					<li class='chooseCar_liStyle' city_id='${data[i].city_id}' city_name='${data[i].city_name}' pname='${pname}' pid='${pid}'>${data[i].city_name}</li>
    					`
    				}
    			}
    			$('#chooseCar_city_list').html(cityHTML);
    			$('#chooseCar_city').addClass('active');
    		})
    		
    	}
    })
    
    //城市被点击时，将内容添加入框内，并影藏省和市的选择弹框
    $('#chooseCar_city_list').on('click','li',function(e){
    	e.stopPropagation();
    	var pname = $(this).attr('pname');
    	var pid = $(this).attr('pid');
    	var city_id = $(this).attr('city_id');
    	var city_name = $(this).attr('city_name');
    	$('#chooseCar_local>p').html(pname+' '+city_name);
    	$('#chooseCar_local>p').attr('cid',city_id);
    	
    	$('#chooseCar_prov').removeClass('active');
		$('#chooseCar_city').removeClass('active');

    })
    
    //当点击区域不在列表选择范围内则关闭弹框
    $(document).click(function(e){  
    	var $this = $(e.target); 
    	if($this.attr('id') != "chooseCar_prov" || $this.attr('id') != "chooseCar_city" || $this.attr('id') != "chooseCar_local"){
			$('#chooseCar_prov').removeClass('active');
			$('#chooseCar_city').removeClass('active');
    	}
    	if( $this.attr('id') == "chooseCar_local" ){
    		$('#chooseCar_prov').addClass('active');
    	}
    });
    
    //提交
    $('#btn_gz').click(function(e){
    	e.stopPropagation();
    	var zone = $('#chooseCar_local>p').attr('cid');//城市id
    	var regDate = $('#chooseCar_year>p').attr('year')+'-'+$('#chooseCar_year>p').attr('mon');//日期
    	var mile = $('#mile').val();
    	var modelId = $('#chooseCar>p').attr('model_id');
    	
    	var modelName = $('#chooseCar>p').html();//车型全称
    	var year = $('#chooseCar_year>p').html();//上牌日期
    	var cityName = $('#chooseCar_city_list>li').html();//城市名
    	
    	var gear_type = $('#chooseCar>p').attr('gear_type');//手动还是自动
    	var liter = $('#chooseCar>p').attr('liter');//排量
    	var discharge_standard = $('#chooseCar>p').attr('discharge_standard');//排放标准
    	var model_price = $('#chooseCar>p').attr('model_price');//新车售价
    	
    	if($('#chooseCar>p').html() == '请选择车型' || $('#chooseCar_year>p').html() =='请选择年份' || $('#chooseCar_local>p').html() =='请选择地域'){
    		alert('信息不完整，请填写信息！');
    	}else{
    		
    		window.sessionStorage['zone'] = zone;//地区id
    		window.sessionStorage['regDate'] = regDate;//日期
    		window.sessionStorage['mile'] = mile;//公里数
    		window.sessionStorage['modelId'] = modelId;//车型id
    		
    		window.sessionStorage['modelName'] = modelName;//车型名称
    		window.sessionStorage['year'] = year;//上牌日期
    		window.sessionStorage['cityName'] = cityName;//城市名
    		
    		window.sessionStorage['gear_type'] = gear_type;//手动还是自动
    		window.sessionStorage['liter'] = liter;//排量
    		window.sessionStorage['discharge_standard'] = discharge_standard;//排放标准
    		window.sessionStorage['model_price'] = model_price;//新车售价
    		
    		window.location.href='/home/appraisement'
    	}
    })
    
    $('#product_info').on('click','.info_sq_btn>a',function(e){
    	e.stopPropagation();
    	window.sessionStorage['dataId'] = $(this).parent().attr('data-id');
    	
    })
    
    

    //省数组去重
    function repeat(arr){
	    var hash={};
	    var result=[];
	    for(var i=0;i<arr.length;i++){
	        if(hash[arr[i].prov_id]===undefined){
	            result.push(arr[i].prov_id);
	            hash[arr[i].prov_id]=1;
	        }
	    }
	    return result;
	}
    //比较器函数
    function compare(a,b){     return a-b   };
});